#!/bin/bash

# This is jCadencii (OS X & Linux) platform tools wrapper


# By default, Yawu was placed in ./build/wavtool-yawu relative.
# If you have moved these files, change it below.
YAWU_DIR="$(dirname "$0")"
YAWU_PATH="${YAWU_DIR}/build/wavtool-yawu"

# You may optionally set jCadencii's resampler setting to this script.
# If so, this script will detect it automatically,
# allowing more flexible configuration.
UTAU_RESAMPLER=(wine 'C:\Program Files\UTAU\resampler.exe')

# The output is redirected to the log file
YAWU_LOG=/tmp/cadencii-yawu.log

if echo -n "$3" | grep -q '[A-G]'
then
    "${UTAU_RESAMPLER[@]}" "$@" 2>&1 | tee -a "${YAWU_LOG}"
else
    "${YAWU_PATH}" "$@" 2>&1 | tee -a "${YAWU_LOG}"

    if [ -e "$1" ]
    then
        # jCadencii does not use the real output file.
        # Instead, it uses two temporary files,
        # which the original wavtool generates during rendering.
        #
        # So let us regenerate these temporary files
        # to satisfy jCadencii.
        head -c 44 "$1" > "$1.whd"
        tail -c +45 "$1" > "$1.dat"
    fi
fi
